#include <stdio.h>
#include <stdlib.h>

int *find_saddlePoint(int (*p)[3],int rows,int cols,int *count)
{
    int rows_max[3];
    int clos_min[3];
    int *saddlePoint = (int*)malloc(3 * sizeof(int));
    *count = 0;
    //找到每一行的最大值
    for(int i = 0;i < rows;i++)
    {
        int max_rows = p[i][0];
        for(int j = 0;j < cols; j++)
        {
            if(p[i][j] > max_rows)
                max_rows = p[i][j];
        }
        rows_max[i] = max_rows;
    }

    //找到每一列的最小值
    for(int j = 0;j < cols; j++)
    {
        int min_cols = p[0][j];
        for(int i = 0;i < rows; i++)
        {
            if(p[i][j] < min_cols)
                min_cols = p[i][j];
        }
        clos_min[j] = min_cols;
    }

    //找出鞍点
    for(int i = 0;i < rows; i++)
    {
        for(int j = 0;j < cols; j++)
        {
            if((p[i][j] == rows_max[i]) && (p[i][j] == clos_min[j]))
            {
                saddlePoint[*count] = p[i][j];
                (*count)++;
            }
        }
    }
    return saddlePoint;
}

int main(int argc, char const *argv[])
{
    int arr[3][3] = {{4,6,6},{3,7,8},{7,8,9}};
    int count = 0;

    int *saddlePoint = find_saddlePoint(arr,3,3,&count);

    if(count == 0)
        printf("没有鞍点！");
    else
    {
        printf("鞍点有%d个\n",count);
        for(int i = 0;i < count;i++)
            printf("%d ",saddlePoint[i]);

        printf("\n");
    }
    free(saddlePoint);

    return 0;
}
